package GUI;

import java.awt.HeadlessException;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;

/**
 * This a main class for MarketManager, all process for MarketManager
 * starts from this class
 * @author michael
 * @author ido
 * @author matan
 * @author sharon
 */
public class MarketManagerFORM extends ClientAbstractFORM
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6120687338269912784L;
	private JButton DealSaleButton = null;
	private JButton customerReportButton = null;
	private JButton responseReportButton = null;
	private JButton CategoryManagementButton = null;
	private JButton addResidencesAreaButton = null;	
	private JButton updateResidencesAreaButton = null;	
	private CategoryManagementForm categorymanagementForm = null;
	private JFrame addResidencesAreaJFrame = null;
	private JFrame updateResidencesAreaJFrame = null;
	private ReportForm reportForm = null;
	
	/**
	 * This is the default constructor
	 */
	public MarketManagerFORM() 
	{
		super();
		initialize();	
	}
	
	/**
	 * This method initializes MarketManagerFORM
	 */
	private void initialize() 
	{
		this.setSize(500, 500);
		this.setLayout(null);
		this.add(getDealSaleButton(),null);
		this.add(getCustomerReportButton(),null);
		this.add(getResponseReportButton(),null);
		this.add(getCategoryManagementButton(),null);
		this.add(getAddResidencesAreaButton() ,null);	
		this.add(getUpdateResidencesAreaButton(),null);		
	}
	
	/**
	 * This method initializes DealSaleButton	
	 */
	private JButton getDealSaleButton() 
	{
		if (DealSaleButton == null) 
		{
			DealSaleButton = new JButton();
			DealSaleButton.setBounds(new Rectangle(20, 20, 172, 29));
			DealSaleButton.setText("Active Campaign");
			DealSaleButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try
					{						
						TemplateSearch templatesearch = new TemplateSearch("Deal Sale");
						templatesearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						templatesearch.setSize( 520, 500 ); // set frame size
						templatesearch.setVisible( true ); // display frame
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return DealSaleButton;
	}
	
	/**
	 * This method initializes customerReportButton
	 */
	private JButton getCustomerReportButton() 
	{
		if (customerReportButton == null) 
		{
			customerReportButton = new JButton();
			customerReportButton.setBounds(new Rectangle(20, 50, 172, 29));
			customerReportButton.setText("Customers Report");
			customerReportButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{	
					try
					{
						ConnectionToServer connectionToServer = new ConnectionToServer();
						String msg = (String)connectionToServer.customerReport();
						if(msg != null)
						{	
							reportForm = new ReportForm();
							JFrame frame = new JFrame("Reports"); 
							frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
							//Add content to the window. 
							frame.add(reportForm); 
							//Display the window. 
							frame.pack();
							frame.setSize(300, 300);
							reportForm.setText(msg);	
							frame.setVisible(true);													
						}	
						else
							JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
					}catch(ClientNotInitializesException exc)
					{
						JOptionPane.showMessageDialog(null, "ERROR","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return customerReportButton;
	}
	
	
	/**
	 * This method initializes responseReportButton
	 */
	private JButton getResponseReportButton() 
	{
		if (responseReportButton == null) 
		{
			responseReportButton = new JButton();
			responseReportButton.setBounds(new Rectangle(20, 80, 172, 29));
			responseReportButton.setText("Response Report");
			responseReportButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					DealSaleSearch dealsalesearch = new DealSaleSearch();
					dealsalesearch.setSize(300, 300);
					dealsalesearch.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					dealsalesearch.setVisible(true);
				}
			});
		}
		return responseReportButton;
	}
	
	/**
	 * This method initializes CategoryManagementButton
	 */
	private JButton getCategoryManagementButton() 
	{
		if (CategoryManagementButton == null) 
		{
			CategoryManagementButton = new JButton();
			CategoryManagementButton.setBounds(new Rectangle(20, 110, 172, 29));
			CategoryManagementButton.setText("Category Management");
			CategoryManagementButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{	
					categorymanagementForm = new CategoryManagementForm();
					categorymanagementForm.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					categorymanagementForm.setVisible(true);
				}
					
			});
		}
		return CategoryManagementButton;
	}
	
	/**
	 * This method initializes addResidencesAreaButton
	 */
	private JButton getAddResidencesAreaButton() 
	{
		if (addResidencesAreaButton == null) 
		{
			addResidencesAreaButton = new JButton();
			addResidencesAreaButton.setBounds(new Rectangle(20, 140, 172, 29));
			addResidencesAreaButton.setText("Add residences area");
			addResidencesAreaButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{	
					ResidencesAreaForm residencesareaForm = null;
					try
					{
						addResidencesAreaJFrame = new JFrame();
						residencesareaForm = new ResidencesAreaForm();						
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
						return;
					}
					addResidencesAreaJFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					addResidencesAreaJFrame.setSize( 300, 220 ); // set frame size
					addResidencesAreaJFrame.setTitle("Add residences area");
					residencesareaForm.addSaveButton();
					addResidencesAreaJFrame.setResizable(false);	
					residencesareaForm.getCancelButton().addActionListener(new java.awt.event.ActionListener() 
					{
						public void actionPerformed(java.awt.event.ActionEvent e) 
						{
							
						}
					});		
					addResidencesAreaJFrame.setContentPane(residencesareaForm);
					addResidencesAreaJFrame.setVisible( true ); // display frame
				}
			});
		}
		return addResidencesAreaButton;
	}	
	
	/**
	 * This method initializes updateResidencesAreaButton
	 */
	private JButton getUpdateResidencesAreaButton() 
	{
		if (updateResidencesAreaButton == null) 
		{
			updateResidencesAreaButton = new JButton();
			updateResidencesAreaButton.setBounds(new Rectangle(20, 170, 172, 29));
			updateResidencesAreaButton.setText("Update residences area");
			updateResidencesAreaButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					ResidencesAreaForm residencesareaForm = null;
					try
					{
						updateResidencesAreaJFrame = new JFrame();
						residencesareaForm = new ResidencesAreaForm();						
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
						return;
					}
					updateResidencesAreaJFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					updateResidencesAreaJFrame.setSize( 300, 220 ); // set frame size
					updateResidencesAreaJFrame.setTitle("Update residences area");
					residencesareaForm.addUpdateButton();
					residencesareaForm.getCancelButton().addActionListener(new java.awt.event.ActionListener() 
					{
						public void actionPerformed(java.awt.event.ActionEvent e) 
						{
												
						}
					});		
					updateResidencesAreaJFrame.setContentPane(residencesareaForm);
					updateResidencesAreaJFrame.setVisible( true ); // display frame
				}
			});			
		}
		return updateResidencesAreaButton;
	}	
}
